package com.yiwei.ydd.activity;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.adapter.MyCommissionDetailAdapter;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.PageBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.constant.Const;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.Spans;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.UI;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.PagesDetailDialog;
import com.yiwei.ydd.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyCommissionDetailActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.txt_sum)
    TextView txtSum;
    @BindView(R.id.txt_collected)
    TextView txtCollected;
    @BindView(R.id.txt_not_collected)
    TextView txtNotCollected;
    @BindView(R.id.btn_money_open)
    ImageView btnMoneyOpen;
    @BindView(R.id.btn_tips)
    ImageView btnTips;
    @BindView(R.id.btn_go)
    TextView btnGo;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private String money_show;
    private PagesDetailDialog pagesDetailDialog;
    private Unbinder unbiner;
    private MyCommissionDetailAdapter adapter;
    private boolean isOkRefresh = false;
    private int currentPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_commission_detail);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    public void getCommissionList(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }


        refresh.setRefreshing(true);
        PageBean bean = new PageBean();
        bean.page = page;
        Api.api_service.getCommissionList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                        isOkRefresh = true;
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    public void getCommissionBasic() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }


        refresh.setRefreshing(true);
        Api.api_service.getCommissionBasic().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    Spanned collected = Spans.with(this).font("已收佣金（元）\n", getResources().getDimensionPixelSize(R.dimen.x26), 0xff999999).
                            font(result.datas.collected, getResources().getDimensionPixelSize(R.dimen.x48), 0xffFB5E01).style(Typeface.BOLD).
                            build();
                    txtCollected.setText(collected);
                    Spanned not_collected = Spans.with(this).font("待收佣金（元）\n", getResources().getDimensionPixelSize(R.dimen.x26), 0xff999999).
                            font(result.datas.not_collected, getResources().getDimensionPixelSize(R.dimen.x48), 0xffFB5E01).style(Typeface.BOLD).
                            build();
                    txtNotCollected.setText(not_collected);

                    money_show = result.datas.sum;
                    txtSum.setText(result.datas.sum);
                });
    }

    private void init() {
        pagesDetailDialog = new PagesDetailDialog(this).setKeyword(Const.PageDetailType.COMMISSION);
        txtTitle.setText("我的佣金明细");
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getCommissionList(1);
                getCommissionBasic();
            }
        });
        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY == (scrollView.getChildAt(0).getMeasuredHeight() - scrollView.getMeasuredHeight())) {
                    //底部加载
                    if (isOkRefresh) {
                        isOkRefresh = false;
                        currentPage++;
                        getCommissionList(currentPage);

                    }
                }
            }
        });
        adapter = new MyCommissionDetailAdapter(this);
        list.setNestedScrollingEnabled(false);
        list.setAdapter(adapter);
        getCommissionBasic();
        getCommissionList(1);
    }

    private void showTips() {
        pagesDetailDialog.show();
//        Dialog.showCommissionTips(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_money_open, R.id.btn_tips, R.id.btn_go})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_money_open:
                if (UI.toString(txtSum).equals("***")) {
                    txtSum.setText(money_show);
                } else {
                    txtSum.setText("***");
                }
                break;
            case R.id.btn_tips:
                showTips();
                break;
            case R.id.btn_go:
                if (Util.getIsLogin(this))
                    Util.startActivity(this, ShareIndexActivity.class);
                break;
            case R.id.btn_back:
                finish();
                break;
        }
    }
}
